package com.md.api.controller;

import com.md.api.config.DuplicateReq;
import com.md.api.entity.HttpResult;
import com.md.api.entity.SendMail;
import com.md.api.entity.dto.MessageSendSmsVo;
import com.md.api.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {


    @Autowired
    private MessageService messageService;




    //发送验证码
    @DuplicateReq(clazz = MessageSendSmsVo.class)
    @RequestMapping(value = "/sendSms",method = RequestMethod.POST)
    @ResponseBody
    public HttpResult sendSms(HttpServletRequest request, @RequestBody MessageSendSmsVo vo) {
        try {
            return messageService.sendSms(request,vo);
        } catch (Exception e) {
            log.error("MessageController.sendSms() happen error! {}", vo, e);
        }
        return HttpResult.fail("驗證碼發送失敗！");
    }


    //邮箱发送验证码
    @DuplicateReq(clazz = SendMail.class)
    @RequestMapping(value = "/sendEmail",method = RequestMethod.POST)
    @ResponseBody
    public HttpResult sendEmail(@RequestBody SendMail vo) {
        try {
            return messageService.sendEmail(vo);
        } catch (Exception e) {
            log.error("MessageController.sendEmail() happen error! {}", vo, e);
        }
        return HttpResult.fail("驗證碼發送失敗！");
    }

    //验证码校验
    @RequestMapping(value = "/smsCheck",method = RequestMethod.POST)
    @ResponseBody
    public HttpResult smsCheck(@RequestBody MessageSendSmsVo vo) {
        try {
            return messageService.smsCheck(vo);
        } catch (Exception e) {
            log.error("MessageController.smsCheck() happen error! {}", vo, e);
        }
        return HttpResult.fail("驗證碼校驗失敗！");
    }

}
